<?php
declare(strict_types=1);
namespace App\Dao;

use App\Model\{Wxservice,WxserviceType,WxserviceOpe};
use Hyperf\DbConnection\Db;

class WxserviceDao
{
    /**
     * 问题反馈运营商编辑提交 OPE
     * @Author abell
     * @DateTime 2020-06-04
     * @param int $id 查询id
     * @return int
     */
    public function opeEditSave($data){
        return WxserviceOpe::query()->where(['id'=>$data['id']])->update($data);
    }

    /**
     * 问题反馈运营商编辑查询 OPE
     * @Author abell
     * @DateTime 2020-06-04
     * @param int $id 查询id
     * @return object
     */
    public function opeEditShow($id){
        return WxserviceOpe::query()->find($id);
    }

    /**
     * 问题反馈运营商添加 OPE
     * @Author abell
     * @DateTime 2020-06-04
     * @param array $data 新增内容数组
     * @param int $pid 默认是0 一级分类,其他则是二级分类
     * @return int
     */
    public function opeAdd($data){
        return WxserviceOpe::query()->insertGetId($data);
    }

    /**
     * 问题反馈运营商获取 OPE
     * @Author abell
     * @DateTime 2020-06-01
     * @param int $type 默认是 1 获取运营商 2 是油卡
     * @return array
     */
    public function opeList(){
        return WxserviceOpe::query()->get()->toArray();
    }

    /**
     * 问题反馈类型删除 TYPE
     * @Author abell
     * @DateTime 2020-05-28
     * @param array $request
     * @return int
     */
    public function typeDelete($id){
        return WxserviceType::query()->where('id',$id)->update(['is_del'=>2,'update_time'=>time()]);
    }

    /**
     * 问题反馈类型查询 TYPE
     * @Author abell
     * @DateTime 2020-05-28
     * @param array $data
     * @return int
     */
    public function typeAdd($data){
        return WxserviceType::query()->insertGetId($data);
    }
    /**
     * 问题反馈类型查询 TYPE
     * @Author abell
     * @DateTime 2020-05-28
     * @param array $request
     * @return int
     */
    public function typeEdit($id,$data){
        return WxserviceType::query()->where('id',$id)->update($data);
    }

    /**
     * 问题反馈类型查询 TYPE
     * @Author abell
     * @DateTime 2020-05-28
     * @param array $request
     * @return array
     */
    public function typeShow($id){
        return WxserviceType::query()->where(['id'=>$id])->get();
    }

    /**
     * 问题反馈类型列表 TYPE
     * @Author abell
     * @DateTime 2020-05-28
     * @param int $pageSize
     * @return array
     */
    public function typeList($select=['*']){
        return WxserviceType::query()->where('is_del',1)->select($select)->get()->toArray();
    }

    /**
     * 问题反馈分类数量 ADMIN
     * @Author abell
     * @DateTime 2020-06-01
     * @param int $type
     * @return int
     */
    public function typeCount($type){
        return Wxservice::query()->where('type',$type)->count();
    }

    /**
     * 编辑问题反馈数据 ADMIN
     * @Author abell
     * @DateTime 2020-05-28
     * @param array $request
     * @return int
     */
    public function editForm($request,$id){
        return Wxservice::query()->where('id',$id)->update($request);
    }

    /**
     * 获取问题反馈数据 ADMIN
     * @Author abell
     * @DateTime 2020-05-28
     * @param int $id
     * @return array
     */
    public function getForm($id){
        return Wxservice::query()->where('id',$id)->get();
    }

    /**
     * 小程序问题反馈列表条数和数据 ADMIN
     * @Author abell
     * @DateTime 2020-05-28
     * @param array $request
     * @return array
     */
    public function formListNum($where,$pageSize,$pageNum=1,$key=null,$start_time=0,$end_time=0){
        $query = Wxservice::query()->where($where);
        if($key){
            // 需要查询时间段
            $data = $query->whereBetween($key,[$start_time,$end_time])
                ->paginate(intval($pageSize),['*'],'page',$pageNum)->toArray();
        }else{
            $data = $query->paginate(intval($pageSize),['*'],'page',$pageNum)->toArray();
        }
        return $data;
    }


    /**
     * 小程序用户提交问题反馈 API
     * @Author abell
     * @DateTime 2020-05-26
     * @param array $request
     * @return int
     */
    public function formMsg($jwt,$request){
        $form_data = [
            'add_time'  => time(),
            'update_time'  => time(),
            'user_id'   => $jwt['uid'],
            'openid'    => $jwt['account'],
            'title'     => $request['title'],
            'mobile'    => $request['mobile'],
            'detail'    => $request['detail'],
            'type_id'    => $request['type_id'],
            'operator_id'    => $request['operator_id'],
            'wxservice_ope_id'    => $request['wxservice_ope_id'],
            'statue'    => 0
        ];
        return Wxservice::query()->insertGetId($form_data);
    }

    /**
     * 小程序问题反馈查询 API
     * @Author abell
     * @DateTime 2020-05-26
     * @param array $request
     * @return array
     */
    public function formInfo($jwt){
        return Wxservice::query()->where(['openid'=>$jwt['account']])->get();
    }

    /**
     * 问题反馈类型列表获取 API
     * @Author abell
     * @DateTime 2020-05-26
     * @param array $request
     * @return array
     */
    public function formType($where){
        return WxserviceType::query()->where($where)->get();
    }


    /**
     * 获取反馈数据排行列表数据
     * @param null $page
     * @param int $pageSize
     * @return array
     */
    public function getCountRankListPage($page=null, $pageSize = 5){
        $data = Wxservice::query()
            ->groupBy(['operator_id'])
            ->orderBy('count', 'desc')->paginate(intval($pageSize), [
                'operator_id',
                Db::raw('COUNT(operator_id) AS `count`')
            ],'page', $page)->toArray();
        $count = Wxservice::query()->groupBy(['operator_id'])->count();
        return [
            'list' => @$data['data'],
            'count' => $count
        ];
    }


    /**
     * 获取反馈数据类型排行列表数据
     * @param null $page
     * @param int $pageSize
     * @return array
     */
    public function getCountTypeRankListPage($page=null, $pageSize = 5){
        $data = Wxservice::query()
            ->groupBy(['type_id'])
            ->orderBy('count', 'desc')->paginate(intval($pageSize), [
                'type_id',
                Db::raw('COUNT(type_id) AS `count`')
            ],'page', $page)->toArray();
        $count = Wxservice::query()->groupBy(['operator_id'])->count();
        return [
            'list' => @$data['data'],
            'count' => $count
        ];
    }
}
